/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import Model.M_News;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tran Thuy Duong
 */
@WebServlet(urlPatterns = {"/C_News"})
public class C_News extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
        
            String action = request.getParameter("NewsAction");
            
            
            if(action.equals("Search"))
            {
                 //M_Customer cus = new M_Customer();
            //M_Customer[] result = cus.show_KH();
                String search=request.getParameter("txtSearch");
            
                M_News n = new M_News();
                M_News[] result = n.search_News(search);
                
                
                request.setAttribute("INFO_search", result);
                
                RequestDispatcher rd = request.getRequestDispatcher("Search_News.jsp");
                rd.forward(request, response);
                
               
            }
            
            else if(action.equals("Show_CenterNews"))
            {
                 //M_Customer cus = new M_Customer();
            //M_Customer[] result = cus.show_KH();
                String search=request.getParameter("CenterNews");
            
                M_News n = new M_News();
                M_News[] result = n.search_News(search);
                
                
                request.setAttribute("INFO_CenterNews", result);
                
                RequestDispatcher rd = request.getRequestDispatcher("RightNews.jsp");
                rd.forward(request, response);
                
               
            }
            
            
            else if(action.equals("Delete"))
            {
                int matt = Integer.parseInt(request.getParameter("matt"));
                //out.println(matt);
                 
                
                M_News news = new M_News();
                news.setMatt(matt);
                boolean result = news.delete_News();
                
                if(result)
               //response.sendRedirect("C_Show_Customer");
                {
                    //out.println("xoa dc");
                    RequestDispatcher rd = request.getRequestDispatcher("C_Show_News");
                    rd.forward(request, response);
                }
       
                
            }
            else if(action.equals("Update"))
            {
                
               int matt = Integer.parseInt(request.getParameter("U_txtmatt"));
               String tieudett = request.getParameter("U_txttieudett");
               String mota = request.getParameter("U_txtmota");
               String ngaydang = request.getParameter("U_txtngaydang");
               String hinhanh = request.getParameter("U_txthinhanh");
               
               
               M_News news = new M_News();
               boolean result = news.update_News(matt, tieudett, mota, ngaydang, hinhanh);
               if(result){
               RequestDispatcher rd = request.getRequestDispatcher("C_Show_News");
               rd.forward(request, response);
               }
              
            }
            else if(action.equals("Insert"))
            {
                
              
               String tieudett = request.getParameter("txttieudett");
               String mota = request.getParameter("txtmota");
               String ngaydang = request.getParameter("txtngaydang");
               String hinhanh = request.getParameter("txthinhanh");
               
               
               M_News news = new M_News();
              //  int matt = news.createMaTT();
               boolean result = news.insert_News(tieudett, mota, ngaydang, hinhanh);
               
               RequestDispatcher rd = request.getRequestDispatcher("C_Show_News");
               rd.forward(request, response);
            }
         
        } finally {
            out.close();
        }
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void alert(String erro) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
